<?php
declare (strict_types=1);

namespace app\store\model\student;

use app\common\model\student\AnswerSheetTopic as AnswerSheetTopicModel;
use think\db\exception\DbException;
use think\Paginator;

class AnswerSheetTopic extends AnswerSheetTopicModel
{
    /**
     * 获取列表
     * @param array $param
     * @return Paginator
     * @throws DbException
     */
    public function getList($param = [])
    {
        // 查询模型
        $query = $this->getNewQuery();
        // 查询参数
        $filter = $this->getQueryFilter($param);

        $order = [
            'answer_sheet_topic_id' => 'asc',
        ];
        // 查询列表记录
        $list = $query
            ->where($filter)
            ->where('is_delete', '=', '0')
            ->order($order)
            ->select();
        return $list;
    }


    public function getQueryFilter($param)
    {
        $params = $this->setQueryDefaultValue($param, [
            'search' => '',
            'answer_sheet_id' => '',
        ]);

        $filter = [];
        !empty($params['search']) && $filter[] = ["subject_name", "like", "%{$params['search']}%"];
        !empty($params['answer_sheet_id']) && $filter[] = ["answer_sheet_id", "=", $params['answer_sheet_id']];
        return $filter;
    }

    public function saveList($questionnaire_id, $data)
    {
        $saveList = [];
        $this->where(['questionnaire_id' => $questionnaire_id])->save(['is_delete' => 1]);
        foreach ($data as $key => $item) {
            if (!empty($item['topic_id'])) {
                $this->where(['topic_id' => $item['topic_id']])->save([
                    'sort' => $key,
                    'questionnaire_id' => $questionnaire_id,
                    'title' => $item['title'],
                    'topic_type' => $item['topic_type'],
                    'scoring_method' => $item['scoring_method'],
                    'fraction' => $item['fraction'],
                    'items' => json_encode($item['items'], 320),
                    'correct_answer' => implode(',', $item['correct_answer']),
                    'is_delete' => 0
                ]);
            } else {
                $saveList[] = [
                    'sort' => $key,
                    'questionnaire_id' => $questionnaire_id,
                    'title' => $item['title'],
                    'topic_type' => $item['topic_type'],
                    'scoring_method' => $item['scoring_method'],
                    'fraction' => $item['fraction'],
                    'items' => json_encode($item['items'], 320),
                    'correct_answer' => implode(',', $item['correct_answer']),
                    'store_id' => self::$storeId,
                    'create_time' => time(),
                    'is_delete' => 0
                ];
            }
        }
        return $this->saveAll($saveList);
    }

    /**
     * 增加记录
     * @param array $data
     * @return bool
     */
    public function add($data)
    {
        if (!empty($data['birthdate'])) {
            $data['birthdate'] = strtotime($data['birthdate']);
        }
        $data['store_id'] = self::$storeId;
        return $this->save($data) ? $this->student_user_id : false;
    }

    /**
     * 更新记录
     * @param array $data
     * @return bool
     */
    public function edit(array $data)
    {
        return $this->save($data);
    }

    /**
     * 软删除
     * @return false|int
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }

}
